package com.bjwl.display.controller.safty;

import com.bjwl.display.exception.SysException;
import com.bjwl.display.service.safty.UserService;
import com.bjwl.display.util.Constants;
import com.bjwl.display.util.Result;
import com.bjwl.display.vo.UserRegisterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * restful风格：
 * get：从服务器取出资源
 * post：在服务器新建一个资源（注册）
 * put：在服务器更新资源
 *delete：从服务器删除资源
 */
@RestController
@RequestMapping("/safty/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterVO userRegisterVO){
        System.out.println(userRegisterVO);
//        System.out.println("user:"+user);
//        System.out.println("role:"+role);
        boolean register;
        try {
            register = userService.register(userRegisterVO);
            if(register==true){
                return Result.success("注册成功");
            }
            return Result.fail("注册失败");
        } catch (SysException e) {
            e.printStackTrace();
            return Result.fail("系统升级中");
        }
    }
    //退出登录
    @GetMapping("/exit")
    public Result exit(HttpSession session){
        //移除sessionId
        try {
            System.out.println("执行推出操作");
            session.removeAttribute(Constants.SESSION_ATTR_CURRUSER);
            return Result.success("您已退出");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统升级中，退出登陆失败！");
        }
    }
}
